package com.mybatis.generatorSql.ui;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import com.mybatis.messages.MybatisSmartCodeHelpBundle;

import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

public class ChooseIfTestParams {
    private JCheckBox checkAll;
    private JPanel paramPanel;
    private JLabel configLabel;
    private JPanel root;
    private final List<String> conditionFields;
    private final Set<JCheckBox> jCheckBoxes = new CopyOnWriteArraySet<>();

    public ChooseIfTestParams(List<String> conditionFields) {
        this.conditionFields = conditionFields;
        $$$setupUI$$$();
        initCheckBox();
        initListener();
        initText();
    }

    private void initText() {
        checkAll.setText(MybatisSmartCodeHelpBundle.message("check.all"));
        configLabel.setText(MybatisSmartCodeHelpBundle.message("config.property.label"));
    }

    private void initCheckBox() {
        if (conditionFields == null || conditionFields.isEmpty()) {
            checkAll.setEnabled(false);
            return;
        }
        int columns = 5;
        int biggerColumn = (int) Math.sqrt(conditionFields.size());
        // 如果 列的总数大于25个, 则取 开根号之后的整形值
        columns = Math.max(biggerColumn, columns);
        int rows = conditionFields.size() / columns + (conditionFields.size() % columns == 0 ? 0 : 1);
        GridLayoutManager mgr = new GridLayoutManager(rows + 1, columns);
        paramPanel.setLayout(mgr);
        paramPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
        for (int i = 0; i < conditionFields.size(); i++) {
            JCheckBox jCheckBox = new JCheckBox();
            GridConstraints constraints = new GridConstraints();
            constraints.setRow(i / columns);
            constraints.setColumn(i % columns);
            constraints.setFill(GridConstraints.FILL_BOTH);
            jCheckBox.setText(conditionFields.get(i));
            jCheckBox.setSelected(true);
            jCheckBoxes.add(jCheckBox);
            paramPanel.add(jCheckBox, constraints);
        }
        final Spacer spacer2 = new Spacer();
        paramPanel.add(spacer2, new GridConstraints(rows, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));

        checkAll.setSelected(true);
    }

    private void initListener() {
        Component[] components = paramPanel.getComponents();
        checkAll.addActionListener(e -> {
            if (components != null && components.length > 0) {
                for (Component component : components) {
                    if (component instanceof JCheckBox) {
                        ((JCheckBox) component).setSelected(checkAll.isSelected());
                    }
                }
            }
        });
        if (components != null && components.length > 0) {
            AtomicBoolean flag = new AtomicBoolean(true);
            for (Component component : components) {
                if (component instanceof JCheckBox) {
                    ((JCheckBox) component).addActionListener(e -> {
                        if (!((JCheckBox) component).isSelected()) {
                            flag.set(false);
                        }
                        //检测每个checkbox的状态，再更新checkAll
                        if (!jCheckBoxes.isEmpty()) {
                            boolean flags = true;
                            for (JCheckBox jCheckBox : jCheckBoxes) {
                                if (!jCheckBox.isSelected()) {
                                    flags = false;
                                    break;
                                }
                            }
                            checkAll.setSelected(flags);
                        }
                    });
                    if (flag.get()) {
                        checkAll.setSelected(flag.get());
                    }
                }
            }
        }
    }

    public Set<String> getConditionFields() {
        return jCheckBoxes.stream().filter(AbstractButton::isSelected).map(AbstractButton::getText).collect(Collectors.toSet());
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        root = new JPanel();
        root.setLayout(new GridLayoutManager(3, 1, new Insets(0, 0, 0, 0), -1, -1));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
        root.add(panel1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        final Spacer spacer1 = new Spacer();
        panel1.add(spacer1, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
        checkAll = new JCheckBox();
        checkAll.setText("全选");
        panel1.add(checkAll, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
        root.add(panel2, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        configLabel = new JLabel();
        configLabel.setText("配置属性");
        panel2.add(configLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JSeparator separator1 = new JSeparator();
        panel2.add(separator1, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        final JScrollPane scrollPane1 = new JScrollPane();
        root.add(scrollPane1, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        paramPanel = new JPanel();
        paramPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        scrollPane1.setViewportView(paramPanel);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return root;
    }

}
